package com.aaa.stream.demo03;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Optional;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/11/5
 */
public class FindMatchMaxMinCount {
    @Test
    public void Test() {
        //假设有个Person类和一个Person列表，现在有两个需求:
        //1.找到年龄大于18岁的人并输出；
        //2.找出所有中国人的数量
        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("上官雪", 18, "中国", 'A'));
        people.add(new Person("慕容复", 14, "法国", 'B'));
        people.add(new Person("逍遥散", 57, "伊拉克", 'B'));
        people.add(new Person("宇文都", 36, "比利时", 'B'));
        people.add(new Person("易烊玺", 45, "中国", 'A'));
        people.add(new Person("无为治", 15, "荷兰", 'A'));

        /*//find操作--findFirst和findAny
        //findFirst过去流中第一个元素
        Optional<Person> first = people.stream().findFirst();
        System.out.println("first = " + first);
        //获取流中任意一个元素，必须使用并行流测试
        Optional<Person> any1 = people.stream().findAny();
        Optional<Person> any = people.parallelStream().findAny();
        System.out.println("any"+any);
        System.out.println("any1 = " + any1);*/

        //match匹配操作--allMatch anyMatch
        //所有元素都必须匹配条件 才返回true
      /*  boolean match = people.stream().allMatch(item -> item.getAge() >= 14);
        System.out.println("match = " + match);*/

        //聚合操作max min count
        //总的个数count
        /*long count = people.stream().count();
        System.out.println("count = " + count);*/
        //max
        /*Optional<Person> max = people.stream().max((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println("年龄的最大值:"+max.get());
        Optional<Person> min = people.stream().min((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println("年龄的最小值:"+min.get());*/

    }
    @Test
    public void test2(){
        //reduce规约操作
        //规约，也称缩减，顾名思义，是把一个流缩减成一个值，能实现对集合求和、求乘积和求最值操作

        Integer[] arr={3,1,5,3,8,6};
        //第一次赋值时，会把集合的前两个元素赋值于t1,t2 后面会把计算的结果赋予t1 在集合的后面的元素赋予t2
        Optional<Integer> reduce = Arrays.stream(arr).reduce((t1, t2) -> t1 += t2);
        System.out.println("reduce = " + reduce);
    }
}


@Data
@NoArgsConstructor
@AllArgsConstructor
class Person{
    private String name;
    private Integer age;
    private String country;
    private char sex;
}